/**
 * 
 */
package org.mips.Sirius.ui;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Locale;

import org.mips.R;
import org.mips.Sirius.components.StopPoint;
import org.mips.Sirius.components.Vehicle;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author mips
 * 
 */
public class SiriusStopMonitoringDialog extends Dialog {

	Context context;
	DateFormat df;
	private RelativeLayout main;
	private Dialog dismissable;
	private ListView list;
	private SeparatedListAdapter adapter;

	/**
	 * @param context
	 */
	public SiriusStopMonitoringDialog(Context context) {
		super(context);
		this.context = context;
		this.df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale
				.getDefault());

		main = new RelativeLayout(context);

		setCanceledOnTouchOutside(true);

		list = new ListView(context);
		list.setChoiceMode(ListView.CHOICE_MODE_NONE);

		TextView listHeader = new TextView(context);
		listHeader.setText("Available lines");
		listHeader.setBackgroundColor(Color.parseColor("#fdd50e"));
		listHeader.setTextColor(Color.BLACK);
		listHeader.setPadding(10, 0, 10, 0);
		listHeader.setGravity(Gravity.CENTER);

		Button button = new Button(context);
		button.setText("Back to map");
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				SiriusStopMonitoringDialog.this.dismiss();

			}
		});

		button.setId(1);
		listHeader.setId(2);

		RelativeLayout.LayoutParams headerParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		headerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

		RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		listParams.addRule(RelativeLayout.ABOVE, button.getId());
		listParams.addRule(RelativeLayout.BELOW, listHeader.getId());

		RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

		main.addView(listHeader, headerParams);
		main.addView(list, listParams);
		main.addView(button, buttonParams);

		setContentView(main, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));

		adapter = new SeparatedListAdapter(context);
	}

	private void drawStop(StopPoint stop) {

		for (Vehicle vehicle : stop.getLastMonitoring()
				.getLastMonitoredVehicles()) {

			ArrayList<String> vehicleInfo = new ArrayList<String>();

			vehicleInfo.add("From: " + vehicle.getOriginName() + " To: "
					+ vehicle.getDestinationName());

			String via = "";
			for (int i = 0; i < vehicle.getViaNames().size(); i++) {
				via += (i == 0 ? "" : ", ") + vehicle.getViaNames().get(i);
			}

			vehicleInfo.add("Via: " + via);

			String arrivalDelay = "";
			int minArrivalDelay = Math.round((vehicle.getTimes()
					.getExpectedArrivalTime().getTime() - vehicle.getTimes()
					.getAimedArrivalTime().getTime()) / 1000f / 60f);
			if (minArrivalDelay > 0) {
				arrivalDelay += "(" + minArrivalDelay + " min. late)";
			} else if (minArrivalDelay < 0) {
				arrivalDelay += "(" + -1 * minArrivalDelay + " min. early)";
			}
			String departureDelay = "";
			int minDepartureDelay = Math.round((vehicle.getTimes()
					.getExpectedDepartureTime().getTime() - vehicle.getTimes()
					.getAimedDepartureTime().getTime()) / 1000f / 60f);
			if (minDepartureDelay > 0) {
				departureDelay += "(" + minDepartureDelay + " min. late)";
			} else if (minDepartureDelay < 0) {
				departureDelay += "(" + -1 * minDepartureDelay + " min. early)";
			}

			vehicleInfo.add("Arrival time: "
					+ df.format(vehicle.getTimes().getExpectedArrivalTime())
					+ " " + arrivalDelay);
			vehicleInfo.add("Departure time: "
					+ df.format(vehicle.getTimes().getExpectedDepartureTime())
					+ " " + departureDelay);

			adapter.addSection(vehicle.getName(), new ArrayAdapter<String>(
					context, R.layout.list_item, vehicleInfo));
		}

		list.setAdapter(adapter);

		setTitle(stop.getName());
	}

	public void setDismissable(Dialog dismissable) {
		this.dismissable = dismissable;
	}

	public void setStop(StopPoint stop) {
		Log.d("tap", "setStop");
		handler.sendMessage(Message.obtain(handler, 0, stop));
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			if (msg.obj != null) {
				SiriusStopMonitoringDialog.this.drawStop((StopPoint) msg.obj);
			}

			Log.d("tap", "dismissing dismissable");
			dismissable.dismiss();

			if (msg.obj != null) {
				SiriusStopMonitoringDialog.this.show();
			}
		}
	};

}
